// System includes
#include <QApplication>
#include <QDesktopWidget>
#include <QPlastiqueStyle>
#include <QPalette>
#include <QDebug>

//Local includes
#include "yigloginmenu.h"
#include "graphics/yigcolorkit.h"


YigLogInMenu::YigLogInMenu(LogInInformation* logIn, QWidget *parent) :
    QDialog(parent),
    mLogIn(logIn)
{
    mLogIn->online = true;
    mLogIn->userName = "octopian";

    QPlastiqueStyle yigStyle;

    formLayout = new QFormLayout;

    QFont menuFont = QFont("monaco", 11);

    onlineRadioButton.setText("Online");
    onlineRadioButton.setStyle(&yigStyle);
    onlineRadioButton.setFont(menuFont);
    QPalette radioPalette = onlineRadioButton.palette();
    radioPalette.setColor(QPalette::Button, QColor(55, 55, 55));
    radioPalette.setColor(QPalette::WindowText, YigColorKit::background2);
    onlineRadioButton.setPalette(radioPalette);

    offlineRadioButton.setText("Offline");
    offlineRadioButton.setStyle(&yigStyle);
    offlineRadioButton.setFont(menuFont);
    radioPalette = offlineRadioButton.palette();
    radioPalette.setColor(QPalette::Button, QColor(55, 55, 55));
    radioPalette.setColor(QPalette::WindowText, YigColorKit::background2);
    offlineRadioButton.setPalette(radioPalette);

    radioLayout.addWidget(&onlineRadioButton);
    radioLayout.addWidget(&offlineRadioButton);
    QObject::connect(&onlineRadioButton, SIGNAL(clicked(bool)), this, SLOT(setOnline(bool)));
    QObject::connect(&offlineRadioButton, SIGNAL(clicked(bool)), this, SLOT(setOffline(bool)));
    connect(&nameEntry,SIGNAL(textChanged(QString)),this,SLOT(changeUserNameTextField(QString)));
    onlineRadioButton.setChecked(true);

    nameEntry.setGeometry(0, 0, 100, 25);
    nameEntry.setStyle(&yigStyle);
    nameEntry.setFont(menuFont);
    nameEntry.setAutoFillBackground(true);
    QObject::connect(&nameEntry, SIGNAL(returnPressed()), this, SLOT(setUserName()));
    nameEntry.setText("octopian");
    QPalette namePalette = nameEntry.palette();
    QColor translucentAccent = YigColorKit::accent;
    translucentAccent.setAlpha(200);
    namePalette.setColor(QPalette::Highlight, translucentAccent);
    //namePalette.setColor(QPalette::Background, YigColorKit::focus1);
    namePalette.setColor(QPalette::Base, Qt::white);
    namePalette.setColor(QPalette::Disabled, QPalette::Base, YigColorKit::focus2Highlight);
    nameEntry.setPalette(namePalette);

    submitButton.setText("Log in");
    submitButton.setStyle(&yigStyle);
    submitButton.setFont(menuFont);
    QObject::connect(&submitButton, SIGNAL(clicked()), this, SLOT(submit()));
    QObject::connect(&submitButton, SIGNAL(pressed()), this, SLOT(submit()));
    QPalette submitPalette = submitButton.palette();

    QLinearGradient gradient(0, 0, 0, submitButton.geometry().height());
    gradient.setColorAt(0, YigColorKit::background2);
    gradient.setColorAt(0.5, QColor(44, 44, 44));
    gradient.setColorAt(0.9, QColor(66, 66, 66));
    gradient.setColorAt(1, YigColorKit::background2);
    gradient.setInterpolationMode(QGradient::ColorInterpolation);
    QBrush buttonBrush(gradient);

    submitPalette.setBrush(QPalette::Button, buttonBrush);
    submitPalette.setColor(QPalette::ButtonText, YigColorKit::focus1);
    submitPalette.setColor(QPalette::Highlight, YigColorKit::focus1);
    submitButton.setPalette(submitPalette);

    formLayout->addRow(tr("Network Mode:"), &radioLayout);
    formLayout->addRow(tr("Screen Name:"), &nameEntry);
    formLayout->addRow(&submitButton);


    QLinearGradient backgroundGradient(0, 0, 0, 100);
    backgroundGradient.setColorAt(0, YigColorKit::focus1);
    backgroundGradient.setColorAt(0.5, YigColorKit::focus1Highlight);
    backgroundGradient.setColorAt(0.9, YigColorKit::focus1Highlight);
    backgroundGradient.setColorAt(1, YigColorKit::focus1);
    backgroundGradient.setInterpolationMode(QGradient::ColorInterpolation);
    QBrush backgroundBrush(backgroundGradient);

    QPalette windowPalette = palette();
    //windowPalette.setColor(QPalette::Window, YigColorKit::focus1Highlight);
    windowPalette.setBrush(QPalette::Window, backgroundBrush);
    setPalette(windowPalette);
    setFont(menuFont);
    setWindowTitle("Yig Log In");
    setWindowFlags(Qt::WindowTitleHint | Qt::CustomizeWindowHint);
    setStyle(&yigStyle);
    setLayout(formLayout);
    adjustSize();
    setFixedSize(size());
    move(QApplication::desktop()->screen()->rect().center() - rect().center());
    show();
    raise();
    exec();
}

YigLogInMenu::~YigLogInMenu()
{
    //delete formLayout;
}

void YigLogInMenu::setOnline(bool online)
{
    mLogIn->online = true;
    offlineRadioButton.setDown(false);
    nameEntry.setDisabled(false);
}

void YigLogInMenu::setOffline(bool online)
{
    mLogIn->online = false;
    onlineRadioButton.setDown(false);
    nameEntry.setDisabled(true);
}

void YigLogInMenu::setUserName()
{
    mLogIn->userName = userNameTextField;
}

void YigLogInMenu::submit()
{
    setUserName();
    close();
}

void YigLogInMenu::changeUserNameTextField(QString userNameTextField)
{
    this->userNameTextField = userNameTextField;
}
